//
//  DataHelper.m
//  AttitudeNews
//
//  Created by 刘晓萌 on 15/11/21.
//  Copyright © 2015年 刘晓萌. All rights reserved.
//
#import "DataHelper.h"

#import "Urls.h"
#import "ANNewsModel.h"
#import "ANNewsInfoNormalModel.h"
#import "ANPicsModel.h"

#import "AFNetworking.h"

#include "ANNewsInfoNormalModel.h"

@interface DataHelper()
@end

@implementation DataHelper

/**
 * 使用AF获取数据列表model
 */
-(void)getnewsModelSFromTitle:(NSString *)newsTitle andPageNumber:(NSInteger)pageNumber{
    //设置URL地址
    NSInteger count = pageShowNumber;
    if (pageNumber < count) {
        pageNumber = count;
    }
    NSString *url = Urls_ANList(newsTitle,pageNumber);
    //NSLog(@"%@",url);
    //创建AF的管理对象类
    AFHTTPRequestOperationManager *myManager = [AFHTTPRequestOperationManager manager];
    myManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    //使用get方式来获取并解析数据
    [myManager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dictionary = responseObject;
        NSMutableArray *newsArray = [NSMutableArray array];
        NSArray *newsArr = dictionary[newsTitle];
        for (NSDictionary *dic in newsArr) {
            ANNewsModel *news = [[ANNewsModel alloc]init];
            [news setValuesForKeysWithDictionary:dic];
            if(news.docid == nil){
                continue;
            }
            if (dic[@"alias"]) {
                news.cellStyle = cellHead;
            }else if (dic[@"imgextra"]){
                news.cellStyle = cellPIC;
            }else if (dic [@"imgType"]){
                news.cellStyle = cellOnePic;
            }else{
                news.cellStyle = cellNormal;
            }
            
            [newsArray addObject:news];
        }
        if (self.ResultBlock) {
            self.ResultBlock(newsArray);
        }
    }
           failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               //NSLog(@"%@",error);
           }
     ];
}

/**
 * 使用AF获取普通数据model
 */
-(void)getANNewsInfoNormalModelByDocid:(NSString *)docid{
    //设置URL地址
    NSString *url = Urls_ANList_SubUrl(docid);
    //NSLog(@"%@",url);
    //创建AF的管理对象类
    AFHTTPRequestOperationManager *myManager = [AFHTTPRequestOperationManager manager];
    myManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    //使用get方式来获取并解析数据
    [myManager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dictionary = responseObject;
        NSDictionary *dic = dictionary[docid];
        ANNewsInfoNormalModel *normalNews = [[ANNewsInfoNormalModel alloc]init];
        [normalNews setValuesForKeysWithDictionary:dic];
        if (self.ResultNormlaNewsInfoBlock) {
            self.ResultNormlaNewsInfoBlock(normalNews);
        }
    }
           failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               //NSLog(@"%@",error);
           }
     ];
}

/**
 * 使用AF获取图集model
 */
-(void)getANPicsModelBySKipId:(NSString *)skipId{
    NSString *strX = [NSString string];
    NSString *strY = [NSString string];
    //设置URL地址
    NSArray *arr =  [skipId componentsSeparatedByString:@"|"];
    if (arr.count == 2) {
        strX = [arr[0] substringFromIndex:4];
        strY = arr[1];
    }
    NSString *url = Urls_PicsUrl(strX,strY);
    //NSLog(@"%@",url);
    //创建AF的管理对象类
    AFHTTPRequestOperationManager *myManager = [AFHTTPRequestOperationManager manager];
    myManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
    //使用get方式来获取并解析数据
    [myManager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dictionary = responseObject;
        NSArray *arr = dictionary[@"photos"];
        NSMutableArray *picArr = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            ANPicsModel *picModel = [[ANPicsModel alloc]init];
            [picModel setValuesForKeysWithDictionary:dic];
            [picArr addObject:picModel];
        }
        if (self.ResultPicsModelBlock) {
            self.ResultPicsModelBlock(picArr);
        }
    }
           failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               //NSLog(@"%@",error);
           }
     ];

}

#pragma mark ---图文混排的信息解析.存储
@end

